package HomeElements;

import android.support.test.uiautomator.By;
import android.support.test.uiautomator.BySelector;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiObject;
import android.support.test.uiautomator.UiObject2;
import android.support.test.uiautomator.UiObjectNotFoundException;
import android.support.test.uiautomator.UiScrollable;
import android.support.test.uiautomator.UiSelector;
import android.support.test.uiautomator.Until;

import static HomeElements.ZuiJinGuanZhuData.zuiJinGuanZhuHotel;

import java.util.List;

import util.GetDevice;

import static HomeElements.BySelectorElementType.*;

/**
 * Created by zhe.xiao on 2017/6/22.
 */

public class Elements {
    private static UiDevice device = GetDevice.getDevice();
    /**
     * 首页
     */
    public static UiObject2 shouYeElement(){
        waitElement(shouYe());
        return device.findObject(shouYe());
    }
    /**
     * 广告
     */
    public static UiObject2 guangGaoElement(){
        UiObject2 ele = swipDownUitilElement(guangGao());
        return ele;
    }

    /**
     * 签证*WiFi
     */
    public static UiObject2 wifiElement(){
        UiObject2 ele = swipDownUitilElement(wifi());
        return ele;
    }

    /**
     * 出境游
     */

    public static UiObject2 chuJingYouElement(){
        UiObject2 ele = swipDownUitilElement(chuJingYou());
        return ele;
    }

    /**
     * 接送机
     */
    public static UiObject2 jieJiSongElement(){
        UiObject2 ele = swipDownUitilElement(jieJiSong());
        return ele;
    }

    /**
     *一日游
     */
    public static UiObject2 yiRiYouElement(){
        UiObject2 ele = swipDownUitilElement(yiRiYou());
        return ele;
    }

    /**
     * 智慧旅行
     */
    public static UiObject2 zhiHuiLvXingElement(){
        UiObject2 ele = swipDownUitilElement(zhiHuiLvXing());
        return ele;
    }

    /**
     * 定制游
     */
    public static UiObject2 dingZhiYouElement(){
        UiObject2 ele = swipDownUitilElement(dingZhiYou());
        return ele;
    }

    /**
     * 特色住
     */
    public static UiObject2 teSeZhuElement(){
        UiObject2 ele = swipDownUitilElement(teSeZhu());
        return ele;
    }

    /**
     * 领券中心
     */
    public static UiObject2 lingQuanZhongXinElement(){
        UiObject2 ele = swipDownUitilElement(lingQuanZhongXin());
        return ele;
    }

    /**
     * 踏青赏花
     */
    public static UiObject2 taQingShangHuaElement(){
        UiObject2 ele = swipDownUitilElement(taQingShangHua());
        return ele;
    }

    /**
     * 特价游
     */
    public static UiObject2 teJiaYouElement(){
        UiObject2 ele = swipDownUitilElement(teJiaYou());
        return ele;
    }

    /**
     * 大搜入口
     */
    public static UiObject2 daSouElement(){
        waitElement(daSou());
        UiObject2 ele = device.findObject(daSou());
        return ele;
    }

    /**
     * 最近关注更多
     */

    public static UiObject2 zuiJinGuanZhuMoreElement(){
        UiObject2 ele = swipDownUitilElement(zuiJinGuanZhuMore()).getParent();
        if(ele == null){
            swipUpUitilElement(hoemHotel());
            zuiJinGuanZhuHotel();
            ele = swipDownUitilElement(zuiJinGuanZhuMore()).getParent();
        }
        UiObject2 more= ele.findObject(By.res("com.mqunar.atom.alexhome:id/atom_alexhome_card_title_more"));
        return more;
    }

    /**
     * 最近关注第一个
     */
    public static UiObject2 zuiJinGuanZhu1Element(){
        UiObject2 ele = swipDownUitilElement(zuiJinGuanZhuMore());
        UiObject2 eleF = ele.getParent().getParent().getParent();
        UiObject2 ele1 = eleF.getChildren().get(1).getChildren().get(0);
        return ele1;
    }

    /**
     * 最近关注第二个
     */
    public static UiObject2 zuiJinGuanZhu2Element(){
        UiObject2 ele = swipDownUitilElement(zuiJinGuanZhuMore());
        UiObject2 eleF = ele.getParent().getParent().getParent();
        UiObject2 ele1 = eleF.getChildren().get(1).getChildren().get(1);
        return ele1;
    }

    /**
     * 您可能想去第一个标签
     */
    public static UiObject ninKeNeng1Element(){
        UiObject ele = swipDownUitilElement(ninKeNeng1tag());
        return ele;
    }

    /**
     * 您可能想去第二个标签
     */
    public static UiObject ninKeNeng2Element(){
        UiObject ele = swipDownUitilElement(ninKeNeng2tag());
        return ele;
    }

    /**
     * 您可能想去第三个标签
     */
    public static UiObject ninKeNeng3Element(){
        UiObject ele = swipDownUitilElement(ninKeNeng3tag());
        return ele;
    }

    /**
     * 您可能想去的全部标签
     */
    public static UiObject2 ninKeNengMoreElement(){
        UiObject2 ele = swipDownUitilElement(ninKeNengMore());
        return ele;
    }

    /**
     * 热门目的地第一个标签
     */
    public static UiObject reMenMu1Element(){
        UiObject ele = swipDownUitilElement(reMenMu1());
        return ele;
    }

    /**
     * 热门目的地第二个标签
     */
    public static UiObject reMenMu2Element(){
        UiObject ele = swipDownUitilElement(reMenMu2());
        return ele;
    }

    /**
     * 热门目的地第三个标签
     */
    public static UiObject reMenMu3Element(){
        UiObject ele = swipDownUitilElement(reMenMu3());
        return ele;
    }

    /**
     * 热门目的地更多
     */
    public static UiObject reMenMuMoreElement(){
        UiObject ele = swipDownUitilElement(reMenMuMore());
        return ele;
    }

    /**
     * 热门专题第一个标签
     */
    public static UiObject2 reMenZhuan1Element(){
        UiObject2 ele = swipDownUitilElement(reMenZhuan1());
        UiObject2 p = ele.getParent().getParent().getParent();
        return p.findObjects(By.res("com.mqunar.atom.alexhome:id/atom_alexhome_search_item_image")).get(0);
    }

    /**
     * 热门专题第二个标签
     */
    public static UiObject2 reMenZhuan2Element(){
        UiObject2 ele = swipDownUitilElement(reMenZhuan2());
        UiObject2 p = ele.getParent().getParent().getParent();
        return p.findObjects(By.res("com.mqunar.atom.alexhome:id/atom_alexhome_search_item_image")).get(1);
    }

    /**
     * 热门专题第三个标签
     */
    public static UiObject2 reMenZhuan3Element(){
        UiObject2 ele = swipDownUitilElement(reMenZhuan3());
        UiObject2 p = ele.getParent().getParent().getParent();
        return p.findObjects(By.res("com.mqunar.atom.alexhome:id/atom_alexhome_search_item_image")).get(2);
    }

    /**
     * 热门专题更多
     */
    public static UiObject2 reMenZhuanMoreElement(){
        UiObject2 ele = swipDownUitilElement(reMenZhuanMore());
        return ele;
    }

    /**
     * 首页可滑动的viewlist
     */
    public static UiSelector homeListViewElement(){
        return homeViewList();
    }

    /**
     * 等待元素出现，等待时间为20秒
     * @param by
     */
    public static void waitElement(BySelector by){
        device.wait(Until.findObject(by), 20000);
    }



    private static UiScrollable getScroller(){
        return new UiScrollable(homeListViewElement());
    }

    /**
     * 向下滑动一屏
     */
    private static void downSwipeOneScreen(){
        try {
            getScroller().scrollForward();
        } catch (UiObjectNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * 向上滑动一屏
     */
    private static void upSwipeOneScreen(){
        try {
            getScroller().scrollBackward();
        } catch (UiObjectNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * 向下滑动
     */
    public static void downSwipe(){
        try {
            getScroller().scrollForward();
        } catch (UiObjectNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * 向下滑动
     */
    public static void upSwipe(){
        try {
            getScroller().scrollBackward();
        } catch (UiObjectNotFoundException e) {
            e.printStackTrace();
        }
    }

    

    /**
     * 向下滑动，直到指定元素出现
     */
    private static UiObject swipDownUitilElement(UiSelector by){
        int y = device.getDisplayHeight() / 2;
        int y1 = device.getDisplayHeight() / 4 * 3;
        int x = device.getDisplayWidth() / 2;
        int cont = 0;
        UiObject ele = device.findObject(by);
        while(true){
            try{
                if(ele.exists()){
                    int i = ele.getVisibleBounds().centerY();
                    if(i > y){
                        device.swipe(x, i, x, y, 20);
                    }
                    int i1 = ele.getVisibleBounds().centerY();
                    if(i1 < y - 500){
                        device.swipe(x, y - 500, x, y, 20);
                    }
                    int i2 = ele.getVisibleBounds().centerY();
                    if(i2 > y1){
                        device.swipe(x, i2, x, y1, 20);
                    }
                    return ele;
                }else{
                    if(cont++ >= 5){
                        return ele;
                    }
                    downSwipeOneScreen();
                    ele = device.findObject(by);
                    continue;
                }
            } catch (UiObjectNotFoundException e) {
                e.printStackTrace();
            }
        }
    }

    private static UiObject2 swipDownUitilElement(BySelector by){
        int y = device.getDisplayHeight() / 2;
        int y1 = device.getDisplayHeight() / 4 * 3;
        int x = device.getDisplayWidth() / 2;
        int cont = 0;
        UiObject2 ele = device.findObject(by);
        while(true){
            try{
                if(ele != null){
                    int i = ele.getVisibleBounds().centerY();
                    if(i > y){
                        device.swipe(x, i, x, y, 20);
                    }
                    int i1 = ele.getVisibleBounds().centerY();
                    if(i1 < y - 500){
                        device.swipe(x, y - 500, x, y, 20);
                    }
                    int i2 = ele.getVisibleBounds().centerY();
                    if(i2 > y1){
                        device.swipe(x, i2, x, y1, 20);
                    }
                    return ele;
                }else{
                    if(cont++ >= 5){
                        return ele;
                    }
                    downSwipeOneScreen();
                    ele = device.findObject(by);
                    continue;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 向上滑动，直到指定元素出现
     */
    private static UiObject swipUpUitilElement(UiSelector by){
        int cont = 0;
        UiObject ele = device.findObject(by);
        while(true){
                if(ele.exists()){
                    return ele;
                }else{
                    if(cont++ >= 5){
                        return ele;
                    }
                    downSwipeOneScreen();
                    ele = device.findObject(by);
                    continue;
                }
        }
    }

    private static UiObject2 swipUpUitilElement(BySelector by){
        int cont = 0;
        UiObject2 ele = device.findObject(by);
        while(true){
            try{
//                ele = device.findObject(by);
                if(ele != null){
                    return ele;
                }else{
                    if(cont++ >= 5){
                        return ele;
                    }
                    upSwipeOneScreen();
                    ele = device.findObject(by);
                    continue;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 最近关注：首页酒店按钮
     */
    public static UiObject2 homeHotelElement(){
        waitElement(hoemHotel());
        UiObject2 ele = device.findObject(hoemHotel());
        return ele;
    }

    /**
     * 最近关注：酒店页面搜索按钮
     */
    public static UiObject2 hotelSearchElement(){
        waitElement(hotelSearch());
        UiObject2 ele = device.findObject(hotelSearch());
        return ele;
    }

    /**
     * 最近关注：酒店单品
     */
    public static List<UiObject2> hotelDanElement(){
        waitElement(hotelDan());
        List<UiObject2> ele = device.findObject(hotelDan()).getChildren();
        return ele;
    }

    /**
     * 最近关注景点类型
     */
    public static UiObject2 daDouSearchElement(){
        waitElement(ticketZuiJinGuanZhu());
        UiObject2 ele = device.findObject(ticketZuiJinGuanZhu());
        return ele;
    }

    public static UiObject2 tickElement(){
        waitElement(ticketSearch());
        UiObject2 ele = device.findObject(ticketSearch());
        return ele;
    }
    public static List<UiObject2> ticketDnaList(){
        waitElement(ticketDan());
        UiObject2 ele = device.findObject(ticketDan());
        List<UiObject2> eleList = ele.getChildren();
        return eleList;
    }

    /**
     * 6+1广告第一个
     */
    public static UiObject2 guangGao1Element(){
        UiObject2 ele = swipDownUitilElement(guangGao6());
        UiObject2 ele1 = ele.getChildren().get(0).getChildren().get(0);
        return ele1;
    }

    /**
     * 6+1广告第二个
     */
    public static UiObject2 guangGao2Element(){
        UiObject2 ele = swipDownUitilElement(guangGao6());
        UiObject2 ele1 = ele.getChildren().get(0).getChildren().get(1).getChildren().get(0);
        return ele1;
    }

    /**
     * 6+1广告第三个
     */
    public static UiObject2 guangGao3Element(){
        UiObject2 ele = swipDownUitilElement(guangGao6());
        UiObject2 ele1 = ele.getChildren().get(0).getChildren().get(1).getChildren().get(1);
        return ele1;
    }

    /**
     * 6+1广告第四个
     */
    public static UiObject2 guangGao4Element(){
        UiObject2 ele = swipDownUitilElement(guangGao6());
        UiObject2 ele1 = ele.getChildren().get(1).getChildren().get(0);
        return ele1;
    }

    /**
     * 6+1广告第五个
     */
    public static UiObject2 guangGao5Element(){
        UiObject2 ele = swipDownUitilElement(guangGao6());
        UiObject2 ele1 = ele.getChildren().get(1).getChildren().get(1);
        return ele1;
    }

    /**
     * 6+1广告第六个
     */
    public static UiObject2 guangGao6Element(){
        UiObject2 ele = swipDownUitilElement(guangGao6());
        UiObject2 ele1 = ele.getChildren().get(1).getChildren().get(2);
        return ele1;
    }

    /**
     * 6+1广告第七个
     */
    public static UiObject2 guangGao7Element(){
        UiObject2 ele = swipDownUitilElement(guangGao6());
        UiObject2 ele1 = ele.getChildren().get(1).getChildren().get(3);
        return ele1;
    }

    /**
     * 6+1广告第八个
     */
    public static UiObject2 guangGao8Element(){
        UiObject2 ele = swipDownUitilElement(guangGao6());
        UiObject2 ele1 = ele.getChildren().get(2).getChildren().get(0);
        return ele1;
    }

    /**
     * 酒店名字按钮
     */
    public static UiObject2 hotelNameElemenet(){
        waitElement(hotelName());
        UiObject2 ele = device.findObject(hotelName());
        return ele;
    }

    /**
     * 点击第一个酒店名字
     */
    public static UiObject2 hotelName1Element(){
        waitElement(hotelName1());
        UiObject2 ele = device.findObject(hotelName1());
        return ele;
    }

    /**
     * 输入名字的按钮
     */
    public static UiObject2 sendHotelNameElement(){
        waitElement(nameSend());
        UiObject2 ele = device.findObject(nameSend());
        return ele;
    }
}
